﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_Vote_RecordController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回投票记录列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
            int voteId=Utils.StringToInt(actionId,0);
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (voteEntity == null)
            {
              return  returnJson = string.Concat("{totalPorperty:0,root:[]}");
            }
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                List<VoteInfo> voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                recordNum = voteList.Count;
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(voteList), "}");
            }
            else
            {
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:[]}");
            }
            return returnJson;
 
        }

        /// <summary>
        /// 查看投票记录 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
            int voteInfoId = (int)Utils.GetQueryAndFormString("id", this, MethodType.Get, 10, 1, DataType.Int);
            int voteId = Utils.StringToInt(actionId,0);
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                List<VoteInfo> voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                VoteInfo voteInfo = new VoteInfo();
                foreach (VoteInfo v in voteList)
                {
                    if (v.Id == voteInfoId)
                    {
                        voteInfo = v;
                        break;
                    }
                }

                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(voteInfo), "}");
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 更新投票记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int voteId = Utils.StringToInt(actionId,0);
            int voteInfoId = (int)Utils.GetQueryAndFormString("id2", this, MethodType.Get, 10, 1, DataType.Int);
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (voteEntity == null)
            {
                return returnJson = "{success:'false'}";

            }
            List<VoteInfo> voteList = new List<VoteInfo>();
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                foreach (VoteInfo v in voteList)
                {
                    if (v.Id == voteInfoId)
                    {
                        v.Title = (string)Utils.GetQueryAndFormString("Title", this, 100, 1, DataType.Str);
                        v.Num = (Int32)Utils.GetQueryAndFormString("Num", this, 10, 0, DataType.Int);
                    }
                }
            }
            voteEntity.VoteText = JsonConvert.SerializeObject(voteList);
            voteEntity.DataTable_Action = DataTableAction.Update;
            if (Normal_VoteSv.Update(voteEntity) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 新增投票记录
        /// </summary>
        /// <returns>json</returns>
        public string Insert(string actionId)
        {
            int voteId = Utils.StringToInt(actionId,0);
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (voteEntity == null)
            {
               return returnJson = "{success:'false'}";

            }
            int voteInfoId = 1;
            List<VoteInfo> voteList = new List<VoteInfo>();
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                 voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                 foreach (VoteInfo v in voteList)
                 {
                     if (voteInfoId < v.Id)
                     {
                         voteInfoId = v.Id;
                     }

                 }
                voteInfoId = voteInfoId + 1;
            }

            VoteInfo voteInfo = new VoteInfo();
            voteInfo.Title = (string)Utils.GetQueryAndFormString("Title", this, 100, 1, DataType.Str);
            voteInfo.Id = voteInfoId;
            voteInfo.Num = (Int32)Utils.GetQueryAndFormString("Num", this, 10, 0, DataType.Int);
            voteList.Add(voteInfo);
            voteEntity.VoteText = JsonConvert.SerializeObject(voteList);
            voteEntity.DataTable_Action = DataTableAction.Update;
            if (Normal_VoteSv.Update(voteEntity) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除投票记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int voteId = Utils.StringToInt(actionId,0);
            int voteInfoId = (int)Utils.GetQueryAndFormString("id", this, MethodType.Get, 10, 1, DataType.Int);
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (voteEntity == null)
            {
                return returnJson = "{success:'false'}";

            }
            List<VoteInfo> voteList = new List<VoteInfo>();
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                VoteInfo voteInfo = new VoteInfo();
                foreach (VoteInfo v in voteList)
                {
                    if (v.Id == voteInfoId)
                    {
                        voteInfo = v;
                        break;
                    }
                }
                voteList.Remove(voteInfo);
            }
            voteEntity.VoteText = JsonConvert.SerializeObject(voteList);
            voteEntity.DataTable_Action = DataTableAction.Update;
            if (Normal_VoteSv.Update(voteEntity) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 批量删除投票记录
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            int voteId = Utils.StringToInt(actionId,0);
            string voteInfoIdList = (string)Utils.GetQueryAndFormString("id", this, MethodType.Get, 100, 1, DataType.Str);
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (!regex.IsMatch(voteInfoIdList))
            {
              return   returnJson = "{success:'false',message:'参数类型不正确'}";

            }
            IDataService<Normal_VoteEntity> Normal_VoteSv = ServiceBuilder.BuildNormal_VoteService();
            Normal_VoteEntity voteEntity = Normal_VoteSv.Look(voteId);
            if (voteEntity == null)
            {
                return returnJson = "{success:'false'}";

            }
            List<VoteInfo> voteList = new List<VoteInfo>();
            List<VoteInfo> voteList2 = new List<VoteInfo>();
            if (!string.IsNullOrEmpty(voteEntity.VoteText))
            {
                voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));

                string[] list = Utils.SplitString(voteInfoIdList, "_");
                string strDel = Utils.ArrayToString(list, ",", false);
                bool isDel = false;
                foreach (VoteInfo v in voteList)
                {
                    for (int i = 0; i < list.Length; i++)
                    {
                        if (v.Id.ToString()== list[i])
                        {
                            isDel = true;
                            break;
                        }
                    }
                    if (!isDel)
                    {
                        voteList2.Add(v);
                    }
                    isDel = false;
                }
            }
            else
            {
            }
            voteEntity.VoteText = JsonConvert.SerializeObject(voteList2);
            voteEntity.DataTable_Action = DataTableAction.Update;
            if (Normal_VoteSv.Update(voteEntity) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;


        }

    }
}
